<title>连接格点</title><link href="http://hzoi.com/tomorrow.css" rel="stylesheet"><div class="ui existing segment"><pre><code><span class="pl-cp">#include</span><span class="pl-cpf">&lt;iostream&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span><span class="pl-cpf">&lt;cstdio&gt;</span><span class="pl-cp"></span>
<span class="pl-cp">#include</span><span class="pl-cpf">&lt;cstring&gt;</span><span class="pl-cp"></span>
<span class="pl-k">using</span> <span class="pl-k">namespace</span> <span class="pl-n">std</span><span class="pl-p">;</span>
<span class="pl-kt">int</span> <span class="pl-n">m</span><span class="pl-p">,</span><span class="pl-n">n</span><span class="pl-p">,</span><span class="pl-n">tot</span><span class="pl-o">=</span><span class="pl-mi">0</span><span class="pl-p">,</span><span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-mi">1000001</span><span class="pl-p">];</span><span class="pl-c1">//此题关键是如何把二维转换成一维，这样才能用并茶几 </span>
<span class="pl-kt">void</span> <span class="pl-nf">init</span><span class="pl-p">();</span><span class="pl-c1">//m*n的矩阵转成一维数组，共有m*n个元素，第i行j列的元素转成一维后 </span>
<span class="pl-kt">int</span> <span class="pl-nf">find</span><span class="pl-p">(</span><span class="pl-kt">int</span><span class="pl-p">);</span><span class="pl-c1">//对应于一位下标为(i-1)*n+j，n表示矩阵共多少列 </span>
<span class="pl-kt">void</span> <span class="pl-nf">merge</span><span class="pl-p">(</span><span class="pl-kt">int</span><span class="pl-p">,</span><span class="pl-kt">int</span><span class="pl-p">);</span>
<span class="pl-kt">void</span> <span class="pl-nf">work</span><span class="pl-p">();</span>
<span class="pl-kt">int</span> <span class="pl-nf">main</span><span class="pl-p">()</span>
<span class="pl-p">{</span>
	<span class="pl-n">init</span><span class="pl-p">();</span>
	<span class="pl-n">work</span><span class="pl-p">();</span>
	<span class="pl-k">return</span> <span class="pl-mi">0</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">init</span><span class="pl-p">()</span>
<span class="pl-p">{</span>
	<span class="pl-n">cin</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">m</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">n</span><span class="pl-p">;</span>
	<span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-n">m</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span><span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-n">i</span><span class="pl-p">]</span><span class="pl-o">=</span><span class="pl-n">i</span><span class="pl-p">;</span><span class="pl-c1">//并查集初始化 </span>
	<span class="pl-k">while</span><span class="pl-p">(</span><span class="pl-o">!</span><span class="pl-n">cin</span><span class="pl-p">.</span><span class="pl-n">eof</span><span class="pl-p">())</span><span class="pl-c1">//当未到文件结束 </span>
	<span class="pl-p">{</span>
		<span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">,</span><span class="pl-n">x1</span><span class="pl-p">,</span><span class="pl-n">x2</span><span class="pl-p">,</span><span class="pl-n">y1</span><span class="pl-p">,</span><span class="pl-n">y2</span><span class="pl-p">;</span>
		<span class="pl-n">cin</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">x1</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">y1</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">x2</span><span class="pl-o">&gt;&gt;</span><span class="pl-n">y2</span><span class="pl-p">;</span>
		<span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-p">(</span><span class="pl-n">x1</span><span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">)</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-o">+</span><span class="pl-n">y1</span><span class="pl-p">;</span><span class="pl-c1">//把(x1,y1)转到一维 </span>
		<span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-p">(</span><span class="pl-n">x2</span><span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">)</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-o">+</span><span class="pl-n">y2</span><span class="pl-p">;</span>
		<span class="pl-n">merge</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">);</span><span class="pl-c1">//合并 </span>
	<span class="pl-p">}</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">work</span><span class="pl-p">()</span>
<span class="pl-p">{</span>
	<span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">;</span>
	<span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-n">n</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span><span class="pl-c1">//先枚举每一条纵边，每列有m条纵边 </span>
	  <span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">j</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">j</span><span class="pl-o">&lt;=</span><span class="pl-n">m</span><span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">j</span><span class="pl-o">++</span><span class="pl-p">)</span><span class="pl-c1">//枚举每条纵边的起始端点 </span>
	  <span class="pl-p">{</span>
	  	  <span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-p">(</span><span class="pl-n">j</span><span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">)</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-o">+</span><span class="pl-n">i</span><span class="pl-p">;</span><span class="pl-c1">//纵边的上端点 </span>
	  	  <span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">j</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-o">+</span><span class="pl-n">i</span><span class="pl-p">;</span><span class="pl-c1">//纵边的下端点 </span>
	  	  <span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">);</span>
	  	  <span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">);</span>
	  	  <span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-o">!=</span><span class="pl-n">y</span><span class="pl-p">)</span><span class="pl-c1">//如果两端点不在一个集合则总数+1，并合并 </span>
	  	  <span class="pl-p">{</span>
	  		  <span class="pl-n">tot</span><span class="pl-o">++</span><span class="pl-p">;</span>
	  		  <span class="pl-n">merge</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">);</span>
	  	  <span class="pl-p">}</span>
	  <span class="pl-p">}</span>
	<span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">i</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">&lt;=</span><span class="pl-n">m</span><span class="pl-p">;</span><span class="pl-n">i</span><span class="pl-o">++</span><span class="pl-p">)</span><span class="pl-c1">//枚举每条横边 </span>
	<span class="pl-p">{</span>
		<span class="pl-k">for</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">j</span><span class="pl-o">=</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">j</span><span class="pl-o">&lt;=</span><span class="pl-n">n</span><span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-n">j</span><span class="pl-o">++</span><span class="pl-p">)</span>
		<span class="pl-p">{</span>
			<span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-p">(</span><span class="pl-n">i</span><span class="pl-o">-</span><span class="pl-mi">1</span><span class="pl-p">)</span><span class="pl-o">*</span><span class="pl-n">n</span><span class="pl-o">+</span><span class="pl-n">j</span><span class="pl-p">;</span><span class="pl-c1">//衡边的左端点 </span>
			<span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">x</span><span class="pl-o">+</span><span class="pl-mi">1</span><span class="pl-p">;</span><span class="pl-c1">//右端点等于左端点+1 </span>
			<span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">);</span>
			<span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">);</span>
			<span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-o">!=</span><span class="pl-n">y</span><span class="pl-p">)</span><span class="pl-c1">//不在一个集合则总数+2，并合并 </span>
			<span class="pl-p">{</span>
				<span class="pl-n">tot</span><span class="pl-o">+=</span><span class="pl-mi">2</span><span class="pl-p">;</span>
				<span class="pl-n">merge</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-n">y</span><span class="pl-p">);</span>
			<span class="pl-p">}</span>
		<span class="pl-p">}</span>
	<span class="pl-p">}</span>
	<span class="pl-n">cout</span><span class="pl-o">&lt;&lt;</span><span class="pl-n">tot</span><span class="pl-o">&lt;&lt;</span><span class="pl-n">endl</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
<span class="pl-kt">int</span> <span class="pl-nf">find</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">)</span>
<span class="pl-p">{</span>
	<span class="pl-k">if</span><span class="pl-p">(</span><span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span><span class="pl-o">==</span><span class="pl-n">x</span><span class="pl-p">)</span><span class="pl-k">return</span> <span class="pl-n">x</span><span class="pl-p">;</span>
	<span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">]);</span>
	<span class="pl-k">return</span> <span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-n">x</span><span class="pl-p">];</span>
<span class="pl-p">}</span>
<span class="pl-kt">void</span> <span class="pl-nf">merge</span><span class="pl-p">(</span><span class="pl-kt">int</span> <span class="pl-n">x</span><span class="pl-p">,</span><span class="pl-kt">int</span> <span class="pl-n">y</span><span class="pl-p">)</span>
<span class="pl-p">{</span>
	<span class="pl-n">x</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">x</span><span class="pl-p">);</span>
	<span class="pl-n">y</span><span class="pl-o">=</span><span class="pl-n">find</span><span class="pl-p">(</span><span class="pl-n">y</span><span class="pl-p">);</span>
	<span class="pl-n">f</span><span class="pl-p">[</span><span class="pl-n">y</span><span class="pl-p">]</span><span class="pl-o">=</span><span class="pl-n">x</span><span class="pl-p">;</span>
<span class="pl-p">}</span>
</cpde></pre></div>